<?php

namespace App\Repositories;

use App\Jobs\Fun;
use App\Models\User\UserModel as User;
use App\Models\User\UserModel;
use App\Models\UserStaffModel as UserStaff;
use App\Models\UserStaffOptionModel as UserStaffOption;

class StaffRepository
{
    protected $userStaff;
    protected $userStaffOption;
    protected $user;

    public function __construct(UserStaff $userStaff, UserStaffOption $userStaffOption, User $user)
    {
        $this->userStaff=$userStaff;
        $this->userStaffOption=$userStaffOption;
        $this->user = $user;
    }

    public function getCreateStaffMustParamRules()
    {
        return [
            'cate' => 'required',
            'realname' => 'required',
            'sex' => 'required|in:1,2',
            'birthday' => 'required|date',
            'education' => 'required|integer',
            'weight' => 'required',
            'political_status' => 'required',
            'native_place' => 'required',
            'nationality' => 'required',
            'marriage' => 'required|in:1,2,3',
            'idcard' => 'required',
            'house' => 'required',
            'image' => 'required',
            'household_address' => 'required',
            'residential_address' => 'required',
            'contact_person' => 'required',
            'contact_phone' => 'required',
            'salary' => 'required|integer',
            'skill' => 'required',
            'hobby' => 'required',
        ];
    }

    public function getCreateUserStaffReloadNeedParams()
    {
        return [
            'cate',
            'realname',
            'sex',
            'birthday',
            'education',
            'weight',
            'political_status',
            'native_place',
            'nationality',
            'marriage',
            'idcard',
            'house',
            'image',
            'household_address',
            'residential_address',
            'contact_person',
            'contact_phone',
            'salary',
            'skill',
            'hobby'
        ];
    }

    public function getCreateUserStaffOptionReloadNeedParams()
    {
        return [
            'edu_bgs',
            'work_historys',
            'familys',
            'job_factorys',
            'job_companys',
        ];
    }

    public function add($uid, array $userStaffData, array $userStaffOptionData)
    {
        $this->userStaff->setRawAttributes($userStaffData);
        $this->userStaff->setAttribute('created_at', time());
        $this->userStaff->setAttribute('updated_at', time());
        $this->userStaff->save();

        $this->userStaffOption->setRawAttributes($userStaffOptionData);
        $this->userStaffOption->setAttribute('created_at', time());
        $this->userStaffOption->setAttribute('updated_at', time());
        $this->userStaffOption->save();

        $this->user->query()->where('id', '=', $uid)->first()->setRawAttributes(['type' => 1, 'updated_at'=>time()])->save();
    }





    /**
     * 获取详情
    */
    public function staff_detail($id)
    {
        $select = [
            'realname as name',//姓名
            'sex', //性别：1，男；2女
            'birthday',
            'education', //最高学历：1小学；2初中；3高中/中专/专科；4本科；5研究生；6博士；7其他
            'weight', //体重
            'political_status',//政治面貌：1群众；2团员；3党员
            'native_place', //籍贯
            'nationality', //民族
            'marriage', //婚姻状况
            'house',//居住情况
            'image',//形象照片
            'idcard',//身份证号
            'household_address',//户口地址
            'contact_person',//经济联系人
            'skill',//专业技能
            'hobby',//兴趣爱好
        ];
        $data = $this->userStaff->newQuery()->where('uid', '=', $id)->select($select)->first()->toArray();
        $user = $this->user->newQuery()->where('id', '=', $id)->first()->toArray();
        $data['phone'] = $user['phone'];//手机号
        $data['age'] = $this->ageByBirthday($data['birthday']);//年龄
        $data['sex']=UserModel::sexTo($data['sex']);
      //  $data['sex']=UserModel::sexTo($data['sex']);
        $data2 = $this->userStaffOption->newQuery()
            ->where('uid', '=', $id)->select([
                'edu_bgs',
                'work_historys', 'familys','comments'
            ])->first();
        if (empty($data2)) {
            $data2 = [
                'edu_bgs' => '',
                'work_historys' => '',
                'familys' => '',
                'comments'=>''
            ];
        }else{
            $data2 = $data2->toArray();

        }
        ///   print_r($data);exit;
        $data = array_merge($data, $data2);
        $data['image']=Fun::pImg($data['image']);
        $data['education']=UserStaff::educationTo($data['education']);
        return $data;



        /**
         'edu_bgs',//教育背景，json数组字符串
         'work_historys'， //工作经历，二维数组，json格式
         'familys' // 家庭成员，二维数组，json格式

         */
       /* $user = $this->user->newQuery()->where('id', '=', $id)->first()->toArray();
        $data = $this->userStaff->newQuery()->where('uid', '=', $id)->select($select)->first()->toArray();
        $data['phone'] = $user['phone'];//手机号
        $data['age'] = $this->ageByBirthday($data['birthday']);//年龄
        $data2 = $this->userStaffOption->newQuery()
            ->where('uid', '=', $id)->select([
                'edu_bgs',
                'work_historys', 'familys','comments'
            ])->first();
        if (empty($data2)) {
            $data2 = [
                'edu_bgs' => '',
                'work_historys' => '',
                'familys' => '',
                'comments'=>''
            ];
        }else{
            $data2 = $data2->toArray();

        }
     ///   print_r($data);exit;
        $data = array_merge($data, $data2);
        $data['image']=Fun::pImg($data['image']);*/
       // print_r($data);exit;
      //  return $data;
    }

    protected function ageByBirthday($birthday)
    {
        $birthdayTime = strtotime($birthday);
        $year = date('Y', $birthdayTime);
        $age =  date('Y') - $year;
        return $age;
    }
}